package cn.itsource.org.controller;

import cn.itsource.basic.domain.BaseQuery;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//@Controller + @ResponseBody
@RestController
@RequestMapping("dept")
/**
 * @Api 作用在类上 value 里面填写的是当前类的文档描述
 */
@Api(value = "部门管理的接口文档",description = "部门管理的接口文档")
public class DeptController {
    @Autowired
    IDepartmentService departmentService;

    /**
     * @ApiOperation 作用在方法上 这个注解里的value 是标注当前方法的作用
     * @ApiParam 作用在参数上 value 是描述当前参数 required
     * @param
     * @return
     */
    /**
     * 一般情况下,但凡传递一个对象,就需要使用psotmapping
     * 只有一个参数或者没有参数就是用get
     * @RequestBody:将请求Json参数转换成java对象
     *  @ResponseBody: 将java对象转换成json返回给前端,返回前端,禁止页面跳转,不走视图解析器
     */
    @ApiOperation(value = "高级分页查询", notes = "高级分页查询")
    @PostMapping("/findPage")
    public PageList findPage(@RequestBody BaseQuery query){
        return departmentService.queryData(query);
    }

    @ApiOperation(value = "查询一条", notes = "单个查询")
    @GetMapping("/findOne")
    public Department findOne(Long id){
        return departmentService.loadById(id);
    }

    //restfull风格发送请求,参数是直接拼接到路径后面
    //从路径上取参数需要两步:
    //1.路径上的参数需要加{}
    //2.参数上需要加 @PathVariable
    @ApiOperation(value = "单个删除", notes = "单个删除")
    @GetMapping("/delete/{id}")
    public void remove(@PathVariable Long id){
        departmentService.remove(id);
    }

    @ApiOperation(value = "树形结构查询", notes = "树形结构查询")
    @GetMapping("/findTree")
    public List<Department> findTree(){
        return departmentService.findTree();
    }

    @ApiOperation(value = "单个新增", notes = "单个新增")
    @PostMapping("/add")
    public void add(@RequestBody @Valid Department department){
        departmentService.save(department);
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @PostMapping("/batchDelete")
    public void batchDelete(@RequestBody List<Long> ids){
        departmentService.batchDelete(ids);
    }

    @ApiOperation(value = "编辑",notes = "编辑")
    @PostMapping("/update")
    public void updateById(@RequestBody Department department){
        departmentService.update(department);
    }

    @ApiOperation(value = "查询所有",notes = "查询所有")
    @GetMapping("/findall")
    public List<Department> findAll(){
        return departmentService.findAll();
    }
}
